// pages/ecergency/editEvent/editEvent.js
var app = getApp()
var util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      title:'',
      eventId:'',//任务ID
      id:'',//任务信息的ID

      //应急事件详情
      attentionItem:'',
      remark:'',
      actionList:[],
      imgList:[],
      bigImgList:[],
      emgHandlePublicGoodsList:[],
      //物资详情
      hiddenGoodsDetail:true,
      emgGoods:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
          eventId:options.eventId,
          title:options.title
      });
    this.getEventContent();
  },
    //获取应急事件的内容信息
    getEventContent:function () {
        let that = this;
        let header= {
            'Cookie': wx.getStorageSync("sessionid"), //读取cookie
            'content-type': 'application/json'
        };
        this.setData({
            id:'',
            attentionItem:'',
            actionList:[],
            emgHandlePublicGoodsList:[],
            imgList:[]
        });
        wx.showLoading()
        wx.request({
            url: app.globalData.baseUrl + '/safe/emgHandlePublic/find',
            header: header,
            data: {eventId: this.data.eventId},
            method: 'post',
            success: function (res) {
                let tempList = res.data.data.list[0].emgHandleListPublics;
                tempList.forEach(function (item) {
                    item.checkFlag=item.status>0;
                });
                that.setData({
                    id:res.data.data.list[0].id,
                    attentionItem:res.data.data.list[0].attentionItem,
                    remark:res.data.data.list[0].remark,
                    actionList:tempList,
                    emgHandlePublicGoodsList:res.data.data.list[0].emgHandlePublicGoodsList
                });
                //关闭加载
                wx.hideLoading()
            },
            fail: function (err) {
                wx.showToast({
                    title: '获取数据失败',
                    icon: 'none',
                })
                //关闭加载
                wx.hideLoading()
            }
        });
        wx.request({
            url: app.globalData.baseUrl + '/safe/file/find',
            header: { 'Cookie': wx.getStorageSync("sessionid"), 'content-type': 'application/x-www-form-urlencoded'},
            data: {contentId: this.data.eventId,contentType:3},
            method: 'post',
            success: function (res) {
                let tempList=res.data.data;
                let bigImgList=[];
                tempList.forEach(function (item) {
                    item.imgUrl=app.globalData.baseUrl + '/safe/file/download?fId=' + item.fId;
                    bigImgList.push(item.imgUrl + '&percentage=1')
                });
               that.setData({
                   imgList:tempList,
                   bigImgList:bigImgList
               })
            },
            fail: function (err) {
            }
        });
    },
    //执行事件里的执行清单
    actionItemClick:function (event) {
        let that = this;
        let header = {
            'Cookie': wx.getStorageSync("sessionid"), //读取cookie
            'content-type': 'application/json'
        };
        let params = {
            id: event.currentTarget.dataset.id,
            handlePublicId: event.currentTarget.dataset.handlePublicId,
            status: event.detail?1:0
        };

        wx.request({
            url: app.globalData.baseUrl + '/safe/emgHandleListPublic/update',
            header: header,
            data: params,
            method: 'post',
            success: function (res) {
                let tempActionList=that.data.actionList;
                tempActionList[event.currentTarget.dataset.index].checkFlag=event.detail;
                that.setData({
                    actionList: tempActionList
                });
            },
            fail: function (res) {
            }
        })

    },
    //上传图片
    uploadImage:function () {
        let that = this;
        wx.chooseImage({
            count: 9, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                let successUp = 0; //成功个数
                let failUp = 0; //失败个数
                let length = res.tempFilePaths.length; //总共个数
                let i = 0; //第几个
                upload(res.tempFilePaths, successUp, failUp, i, length,that);
            }
        })
    },
    //预览图片
    handleImagePreview:function (e) {
        const idx = e.target.dataset.idx
        const images = this.data.bigImgList
        wx.previewImage({
            current: images[idx],  //当前预览的图片
            urls: images,  //所有要预览的图片
        })
    },
    //删除图片
    deleteImage:function (e) {
        let that = this;
        let header = {
            'Cookie': wx.getStorageSync("sessionid"), //读取cookie
            'content-type': 'application/x-www-form-urlencoded'
        };

        wx.request({
            url: app.globalData.baseUrl + '/safe/file/delete',
            header: header,
            data: {fId:e.currentTarget.dataset.fid},
            method: 'post',
            success: function (res) {
                if(res.data.success){
                    let tempList=that.data.imgList;
                    let bigImgList=that.data.bigImgList;
                    tempList.splice(e.currentTarget.dataset.idx,1);
                    bigImgList.splice(e.currentTarget.dataset.idx,1);
                    that.setData({
                        imgList:tempList,
                        bigImgList:bigImgList
                    });
                    wx.showToast({
                        title: '删除成功',
                        icon: 'none',
                    })
                }else{
                    wx.showToast({
                        title: '删除失败',
                        icon: 'none',
                    })
                }
            },
            fail: function (res) {
                wx.showToast({
                    title: '删除失败',
                    icon: 'none',
                })
            }
        })
    },
    //显示物资详情
    showGoodsDetail:function (e) {
        let name = e.currentTarget.dataset.goodsName;
        let that = this;
        this.setData({
            hiddenGoodsDetail: false
        });
        let header = {
            'Cookie': wx.getStorageSync("sessionid"), //读取cookie
            'content-type': 'application/json'
        };
        wx.request({
            url: app.globalData.baseUrl + '/safe/emgGoods/find',
            header: header,
            data: { goodsName: name },
            method: 'get',
            success: function (res) {
                that.setData({
                    emgGoods: res.data.data.list
                })
            },
            fail: function (res) {
            },
        })
    },
    //关闭物资详情
    confirmGoodsDetail:function () {
        this.setData({
            hiddenGoodsDetail: true
        })
    },
    //填写备注
    editRemark:function (e) {
        this.setData({
            remark:e.detail.value
        })
    },
    //保存事件
    saveEvent:function () {
        let that = this;
        let header = {
            'Cookie': wx.getStorageSync("sessionid"), //读取cookie
            'content-type': 'application/json'
        };
        let params = {
            id:this.data.id,
            remark: this.data.remark,
            eventId: this.data.eventId
        };

        wx.request({
            url: app.globalData.baseUrl + '/safe/emgHandlePublic/update',
            header: header,
            data: params,
            method: 'post',
            success: function (res) {
                wx.showModal({
                    title: '提示',
                    content: '保存成功',
                    showCancel: false
                });
                wx.navigateTo({
                    url: '/pages/index/index'
                })
            },
            fail: function (res) {
                wx.showModal({
                    title: '提示',
                    content: '保存失败',
                    showCancel: false
                });
            }
        })
    },
    //完结事件
    finishEvent:function () {
        let that = this;
        let header = {
            'Cookie': wx.getStorageSync("sessionid"), //读取cookie
            'content-type': 'application/json'
        };
        let params = {
            id: that.data.eventId,
            status:1
        };

        wx.request({
            url: app.globalData.baseUrl + '/safe/emgHandlePublic/update',
            header: header,
            data: {remark: this.data.remark, eventId: this.data.eventId, id:this.data.id},
            method: 'post',
            success: function (res) {
                wx.request({
                    url: app.globalData.baseUrl + '/safe/emgEvent/update',
                    header: header,
                    data: params,
                    method: 'post',
                    success: function (res) {
                        wx.showModal({
                            title: '提示',
                            content: '完结成功',
                            showCancel: false
                        });
                        wx.navigateTo({
                            url: '/pages/index/index'
                        })
                    },
                    fail: function (res) {
                        wx.showModal({
                            title: '提示',
                            content: '完结失败',
                            showCancel: false
                        });
                    }
                })
            },
            fail: function (res) {
                wx.showModal({
                    title: '提示',
                    content: '保存失败',
                    showCancel: false
                });
            }
        })

    }
})

function upload(path, successUp, failUp, i, length, page) {
    let header = {
        'Cookie': wx.getStorageSync("sessionid"), //读取cookie
        "Content-Type": "multipart/form-data"
    };
    let params = {
        contentType: 3,
        contentId: page.data.eventId
    };
    wx.showToast({
        icon: "loading",
        title: "正在上传"
    });
    if(length!==0){
        wx.uploadFile({
            url: app.globalData.baseUrl + "/safe/file/upload",
            method: 'post',
            header: header,
            filePath: path[i],
            name: 'file',
            formData: params,
            success: function (res) {
                successUp++;
                if (res.statusCode !== 200) {
                    //关闭加载
                    wx.hideLoading()
                    wx.showModal({
                        title: '提示',
                        content: '上传文件失败',
                        showCancel: false
                    });
                    return 0;
                } else {

                    // let tempList=res.data.data;
                    // let bigImgList=[];
                    // for(let i=0;i<tempList.length;i++){
                    //     tempList[i].imgUrl=app.globalData.baseUrl + '/safe/file/download?fId=' + item.fId;
                    //     bigImgList.push(tempList[i].imgUrl + '&percentage=1')
                    // }
                    // page.setData({
                    //     imgList:tempList.concat(page.data.imgList),
                    //     bigImgList:bigImgList.concat(page.data.bigImgList)
                    // });
                    // console.log(tempList)
                    // //关闭加载
                    // wx.hideLoading()
                    // wx.showModal({
                    //     title: '提示',
                    //     content: '上传文件成功',
                    //     showCancel: false
                    // })
                }

            },
            fail: function (e) {
                failUp++;
                //关闭加载
                wx.hideLoading()
                wx.showModal({
                    title: '提示',
                    content: '上传文件失败',
                    showCancel: false
                });
                return 0;
            }
        })
    }
}